package cn.softgame.jserver.application.chat;

import cn.softgame.jserver.application.Connection;
import cn.softgame.jserver.application.connection.ConnectionFactory;

public class ChatTCPServer implements Runnable{

	private ConnectionFactory connFactory = ConnectionFactory.getInstance(ConnectionFactory.TCPSERVER);
	
	@Override
	public void run() {
		try{
			while(true){
				tcpConnect();
				Thread.sleep(300L);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	private void tcpConnect() throws Exception {
		Connection conn = connFactory.getTcpConnection(true);
		
		String clientMsg = conn.readString();
	    System.out.println("clientMsg==" + clientMsg);
	    
	    StringBuffer buff = new StringBuffer(clientMsg);
	    buff.append("come back again!");
	    
	    String newclientMsg = buff.toString();
	    conn.writeString(newclientMsg);
	    System.out.println("send :" + newclientMsg);
	}

	public static void main(String[] args) {
		Thread chat = new Thread(new ChatTCPServer());
		chat.start();
	}
}
